package com.lsx143.wordcount.day10;

import com.lsx143.wordcount.bean.WaterSensor;
import com.lsx143.wordcount.util.MyUtil;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.table.descriptors.Csv;
import org.apache.flink.table.descriptors.FileSystem;
import org.apache.flink.table.descriptors.Schema;

public class Flink_Table_Connector_Write {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env);
        Schema schema = MyUtil.getSchemaByBean(WaterSensor.class);
        tableEnv.connect(new FileSystem().path("input/sensor.txt"))
                .withFormat(new Csv().fieldDelimiter(',').lineDelimiter("\n"))
                .withSchema(schema)
                .createTemporaryTable("sensor");
        tableEnv
                .connect(new FileSystem().path("output/sensor_id.txt"))
                .withFormat(new Csv())
                .withSchema(schema)
                .createTemporaryTable("sensor_out");

        Table resultTable = tableEnv.sqlQuery("select * from sensor where id='sensor_1'");
        resultTable.executeInsert("sensor_out").print();

        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}